using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MVC.Models.Database;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using MVC.Models;

namespace MVC.Controllers
{
    // 特性路由
    // [Route("/")]
    // [Route("[controller]/[action]")]
    [Authorize]
    public class ShareController : Controller
    {
        private readonly LifengContext _context;
        public ShareController(LifengContext context)
        {
            _context = context;
        }
        // [Route("/")]

        [Route("/[controller]")]
        public IActionResult Index()
        {
            return View();
        }

        //添加文章
        ///<summary>
        ///添加文章
        ///<summary>
        [HttpPost]
        [Route("/api/articles")]
        public IActionResult AddArticle(AddArticle request)
        {
            Random random = new Random();
            // 添加文章
            var model = new Article
            {
                ArticleId = random.Next(1, 999),
                ArticleTitle = request.ArticleTitle,
                ArticleBody = request.ArticleBody,
                ArticleAuthor = request.ArticleAuthor,
                CreateTime = DateTime.Now
            };
            _context.Articles.Add(model);
            _context.SaveChanges();
            return Json(new { code = 200, msg = "添加成功" });
        }

    }
}